package com.example.demo.demos.replaceKey;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static com.example.demo.DemoApplication.rootPath;
import static com.example.demo.demos.replaceKey.ReadExcel.excelKey;
import static com.example.demo.demos.replaceKey.ReadExcel.startTime;

/**
 * 复制文件夹并读取替换key
 * @Author: lixz
 * @Date: 2024/07/25/20:17
 */
//@Component
public class ReadTypeJs implements ApplicationRunner {

//    private static final List<String> keys = List.of("name","key","dataIndex");
    private static final List<String> keys = new ArrayList<>();
    private static final Logger log = LoggerFactory.getLogger(ReadTypeJs.class);

    public static void createdDir() throws Exception {
        File[] files = new File(rootPath).listFiles();
        assert files != null;
        for (File file : files) {
            //判断是否为文件夹 并且不是当前程序所在文件夹
            if (file.isDirectory() && !System.getProperty("user.dir").equals(file.getAbsolutePath())) {
                String newDirPath = file.getCanonicalPath() + "New";
                Path dirPath = Paths.get(newDirPath);
                if (Files.notExists(dirPath)) {
                    Files.createDirectory(dirPath);
                }
                for (File children : Objects.requireNonNull(file.listFiles())) {
                    //新文件url
                    String newFilePath = newDirPath + File.separator + children.getName();
                    Path path = Files.createFile(Paths.get(newFilePath));

                    //替换文件key
                    byte[] bytes = readFile(children);

                    //写入新文件
                    OutputStream outputStream = Files.newOutputStream(path);
                    outputStream.write(bytes);
                    outputStream.close();
                }
            }
        }
    }

    public static byte[] readFile(File file) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        while ((line = reader.readLine()) != null) {
            for (String key : keys) {
                if (line.contains(key)) {
                    String oldStr = line.substring(line.indexOf(":") + 3, line.lastIndexOf('"') == -1 ? line.lastIndexOf("'") : line.lastIndexOf('"'));
                    if (!StringUtils.isBlank(oldStr) && excelKey.containsKey(oldStr)) {
                        line = line.replace(oldStr, String.valueOf(excelKey.get(oldStr)));
                    } else {
                        line = line + "    //todo";
                    }
                }
            }
            sb.append(line);
            sb.append("\n");
        }
        return sb.toString().getBytes();
    }


    @Order(value = 2)
    @Override
    public void run(ApplicationArguments args) throws Exception {
        createdDir();
        long endTime = System.nanoTime();
        long seconds = TimeUnit.NANOSECONDS.toSeconds(endTime-startTime);
        log.info("生成文件总时间:{}",seconds);
    }
}
